package org.finesys.common.redisson.lockResover.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.finesys.common.redisson.annotation.DistributedLock;
import org.finesys.common.redisson.entity.LockEntity;
import org.finesys.common.redisson.lockResover.DistributedLockResolver;
import org.springframework.util.StringUtils;

/**
 * 默认的key解析器,使用方法名和参数进行md5加密生成key
 */
public class DefaultDistributedLockKeyResolver implements DistributedLockResolver {
    @Override
    public LockEntity resolver(ProceedingJoinPoint proceedingJoinPoint, DistributedLock distributedLock) {
        //获取锁的名称
        String lockName = distributedLock.lockName();
        //如果不存在，则用方法名和参数生成
        if (!StringUtils.hasText(lockName)) {
            String methodName = proceedingJoinPoint.getSignature().getName();
            String argsStr = StrUtil.join(StrUtil.COMMA, proceedingJoinPoint.getArgs());
            lockName = SecureUtil.md5(methodName + argsStr);
        }
        return new LockEntity().setLockName(lockName).setLeaseTime(distributedLock.leaseTime()).setWaitTime(distributedLock.waitTime()).setTimeUnit(distributedLock.timeUnit());
    }
}
